/**
 * 自定义原创案例
 * */

var formList = [];
var cateAry = [];


//列表
function getOption(){
    $.ajax({
        type: apis.caseCateOption.method,
        url: apis.caseCateOption.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            cateAry = res.data.cate;
            var h = '', i;
            h += '<option value="">';
            h += '所有';
            h += '</option>';
            for (i = 0; i < cateAry.length; i ++) {
                if (cateid == cateAry[i].id) {
                    h += '<option value="' + cateAry[i].id + '" selected>';
                } else {
                    h += '<option value="' + cateAry[i].id + '">';
                }
                h += cateAry[i].title;
                h += '</option>';
            }
            $('#cateid').html(h);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//列表
function getIndex(){
    var formData = {
        start: pages.start,
        limit: pages.limit
    };
    $.ajax({
        type: apis.casexqIndex.method,
        url: apis.casexqIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            formList = res.data;
            $('#pageTotal').html(res.total);
            indexHtml();
            var pageObj = pages; pageObj.total = res.total;
            pageHtml(pageObj);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//列表字符串
function indexHtml(){
    if (formList.length == 0) {
        msgPop({msg: '没有数据'}); return
    }
    var d = formList;
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].cate_name + '</td>';
        h += '<td>' + d[i].com_name + '</td>';
        h += '<td>';
        if (d[i].com_logo == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].com_logo + '" height="50"/>';
        }
        h += '</td>';
        h += '<td>' + d[i].seller_name + '</td>';
        h += '<td>' + d[i].yaoqiu + '</td>';
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].start_day + '-' + d[i].end_day + '</td>';
        h += '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="case_edit(\'编辑\',\'casexq-edit.html\', ' + d[i].id + ')" style="text-decoration:none">';
        h += '修改';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        //h += '<a href="javascript:;" onclick="case_show(\'查看详情\',\'case-show.html\', ' + d[i].id + ');">';
        //h += '详情';
        //h += '</a>';
        //h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="case_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//编辑弹窗
function case_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


////详情弹窗
//function case_show(title,url,id,w,h){
//    url += '?id=' + id;
//    layer_show(title,url,w,h);
//}


//编辑获取
function getOne(v){
    $.ajax({
        type: apis.casexqOne.method,
        url: apis.casexqOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i, thumb = '';
            $('#title')[0].value = d.title;
            //分类
            h = '';
            for (i = 0; i < cateAry.length; i ++) {
                h += '<option value="' + cateAry[i].id + '">';
                h += cateAry[i].title;
                h += '</option>';
            }
            $('#cateid').html(h);
            $('#cname')[0].value = d.com_name;
            $('#yaoqiu')[0].value = d.yaoqiu;
            $('#start_day')[0].value = d.start_day;
            $('#end_day')[0].value = d.end_day;
            $('#price')[0].value = d.price;
            //详情
            ue.setContent(d.item_detail);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


////详情获取
//function showOne(v){
//    $.ajax({
//        type: apis.caseOne.method,
//        url: apis.caseOne.url,
//        data: { id: v },
//        success: function (res) {
//            console.log(res);
//            if (res.code < 0) {
//                msgPop({msg: res.msg});
//                return
//            }
//            var d = res.data;
//            var h = '', i, thumb = '';
//            $('#title').html(d.title);
//            $('#com_name').html(d.com_name);
//            //封面图
//            h = '';
//            if (d.thumb != '') {
//                h += '<img src="' + d.thumb + '" style="max-width: 500px;"/>';
//                $('#thumb').html(h);
//            }
//            $('#buyer_name').html(d.buyer_name);
//            $('#cate_name').html(d.cate_name);
//            $('#address').html(d.address);
//            $('#designer_name').html(d.designer_name);
//            $('#item_area').html(d.item_area);
//            $('#main_material').html(d.main_material);
//            $('#item_detail').html(d.item_detail);
//            $('#item_finish_time').html(d.item_finish_time);
//            $('#like_num').html(d.like_num);
//            $('#collect_num').html(d.collect_num);
//            $('#forward_num').html(d.forward_num);
//        },
//        error: function () {
//            console.log('错误啦');
//        }
//    });
//}


////显示图片
//function showImg(e){
//    var h = '', i;
//    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
//    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
//    $('#bigShow').html(h).height(e.h);
//}


//保存
function modify(){
    var img = $('#img').val();
    var formData = {
        id: id,
        title: $('#title').val(),
        cateid: $('#cateid').val(),
        cname: $('#cname').val(),
        yaoqiu: $('#yaoqiu').val(),
        price: $('#price').val(),
        start_day: $('#start_day').val(),
        end_day: $('#end_day').val(),
        img: img, cnt: ue.getContent(),
    };
    if (!formData.title) {
        msgPop({msg: '项目名称必须的'}); return
    }
    if (!formData.cateid || formData.cateid == 0) {
        msgPop({msg: '项目分类必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.casexqModify.method,
        url: apis.casexqModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//删除
function case_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.casexqDel.method,
            url: apis.casexqDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}